package com.file;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class FileText {
	private static String[] list2;

	public static void main(String[] args) throws IOException {
		// 第一题

		File xiaoqi = new File("C:/");
		String[] list = xiaoqi.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (dir.isDirectory()) {
					return true;
				}
				return false;
			}
		});

		for (String string : list) {
			xiaoqi = new File("c:/" + string);
			String[] list2 = xiaoqi.list(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					if (name.endsWith(".exe")) {
						return true;
					}
					return false;
				}
			});

			if (list2 != null) {
				for (String string2 : list2) {
					System.out.println(string2);
				}
			}
		}

		// 第二题
		/*
		 * File fordel = new File("d:/"); File file = new File(fordel, "tools");
		 * file.mkdir(); file = new File(fordel, "docs"); file.mkdir(); file =
		 * new File(fordel + "code/", "person"); file.mkdirs(); file = new
		 * File(fordel + "code/", "homework"); file.mkdirs(); file = new
		 * File(fordel + "code/", "green"); file.mkdirs();
		 */
		// 第三题

		/*
		 * File file = new File("."); //OutPut(file); ListAllFile(file);
		 * System.out.println(file.getAbsolutePath());
		 */

		// 第四题
		/*
		 * File file = new File("c:/text.txt"); file.createNewFile();
		 * file.renameTo(new File("d:/demo.txt"));
		 */
		// 第五题
		/*
		 * File name = new File("c:/windows/temp"); String[] list = name.list();
		 * for (int i = 0; i < list.length; i++) { File file = new
		 * File(name.getParent() + "/" + name.getName() + "/" + list[i]);
		 * file.delete(); }
		 */
		// 第六题
		/*
		 * File file = new File("c:/windows/temp"); File.createTempFile("temp",
		 * "last", file); String[] list = file.list(); for (int i = 0; i <
		 * list.length; i++) { file = new File("c:/windows/temp"); file = new
		 * File(file, list[i]); file.deleteOnExit(); }
		 */
		// 第七题

		File file = new File("d:\\text.txt");
		file.createNewFile();
		File file1 = new File("e:/text.txt");
		file1.createNewFile();
		File newFile = new File("d:/", "demo.txt");
		newFile.createNewFile();
		File folder = new File("d:/");
		File tempFile = new File(folder, "folder.txt");
		tempFile.createNewFile();

		// 第八题
		/*
		 * File file=new File("c:/windows/"); String[] list = file.list();
		 * file=new File("c:/windows/"+list[30]);
		 * System.out.println("文件绝对路径: "+file.getAbsolutePath());
		 * System.out.println("文件是否可读: "+file.canRead());
		 * System.out.println("文件是否可写: "+file.canWrite());
		 * System.out.println("文件的大小为: "+file.length()+"byte");
		 */
		// 第九题
		/*
		 * String string = "123456"; char[] charArray = string.toCharArray();
		 * System.out.println(new String(charArray));
		 */
		// 第十题
		/*
		 * String string="123456"; byte[] bytes = string.getBytes();
		 * System.out.println(new String(bytes));
		 */

		// 第十一题
		/*
		 * String string = "易通软件"; byte[] bytes = string.getBytes("UTF-8");
		 * String utf = new String(bytes, "UTF-8"); System.out.println(utf);
		 */

	}

	private static void ListAllFile(File file) {
		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				if (listFiles[i].canExecute()) {
					ListAllFile(listFiles[i]);
				}
				if (listFiles[i].getName().endsWith(".java")) {
					System.out.println(listFiles[i]);
				}
			}
		}
	}

	private static void OutPut(File file) {
		File[] listFiles = file.listFiles();
		for (File file2 : listFiles) {
			if (file2.isDirectory()) {
				OutPut(file2);
			}
			if (file2.getName().endsWith(".java")) {
				System.out.println(file2);
			}
		}
	}
}
